package com.seari.custody.service.Impl;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.seari.custody.bean.*;
import com.seari.custody.config.CaffeineConfig;
import com.seari.custody.mapper.MeetingUserMapper;
import com.seari.custody.service.MeetingService;
import com.seari.custody.service.MeetingUserService;
import com.seari.custody.service.MessageService;
import com.seari.custody.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class MeetingUserServiceImpl implements MeetingUserService {
    @Autowired
    private MeetingUserMapper meetingUserMapper;
    @Autowired
    private MessageService messageService;
    @Autowired
    private MeetingService meetingService;
    @Autowired
    private UserService userService;


    @Override
    @CacheEvict(cacheNames = CaffeineConfig.MEETING_USERS, allEntries = true)

    public Integer batchInsert(List<MeetingUser> meetingUsers) {
        return meetingUserMapper.batchInsert(meetingUsers);
    }

    @Override
    public List<MeetingUser> getMeetingsByUserNumber(String userId) {
        MeetingUserExample meetingUserExample = new MeetingUserExample();
        meetingUserExample.createCriteria().andUserIdEqualTo(userId);
        return meetingUserMapper.selectByExample(meetingUserExample);
    }

    @Override
    @Cacheable(keyGenerator = "myKeyGenerator", sync = true, cacheNames = CaffeineConfig.MEETING_USERS)
    public List<MeetingUser> getMeetingsByMeetingId(String meetingId) {
        MeetingUserExample meetingUserExample = new MeetingUserExample();
        meetingUserExample.createCriteria().andMeetingIdEqualTo(meetingId);
        return meetingUserMapper.selectByExample(meetingUserExample);
    }

    @Override
    @CacheEvict(cacheNames = CaffeineConfig.MEETING_USERS, allEntries = true)
    public Integer updateMeetingStatus(MeetingAck meetingAck) {


        Message byId = messageService.getById(meetingAck.getMId());


        MeetingUser meetingUser = new MeetingUser();
        meetingUser.setMeetingStatus(meetingAck.getMeetingStatus());

        MeetingUserExample meetingUserExample = new MeetingUserExample();
        meetingUserExample.createCriteria().andUserIdEqualTo(byId.getRecvUserId()).andMeetingIdEqualTo(byId.getAssociatedId());

        int i = meetingUserMapper.updateByExampleSelective(meetingUser, meetingUserExample);
        //与会者消息ack 反馈给会议发送人
        Message message = new Message();
        message.setSendUserId(byId.getRecvUserId());
        message.setRecvUserId(byId.getSendUserId());
        message.setAssociatedId(byId.getAssociatedId());
        //获取会议详情
        Meeting meeting = meetingService.selectCzHrtableById(byId.getAssociatedId(),"");

        String name = userService.qryNameByUserNumber(byId.getRecvUserId());

        String msg = "消息通知:" + name + ((1 == meetingAck.getMeetingStatus()) ? "" : "不") + "同意参加您于" + DateUtil.format(meeting.getStartTime(), DatePattern.CHINESE_DATE_TIME_PATTERN) + "主题为:" + meeting.getMeetingHeadline() + "的会议";
        message.setContent(msg);
        messageService.saveMessage(message);

        return i;
    }

    @Override
    @CacheEvict(cacheNames = CaffeineConfig.MEETING_USERS, allEntries = true)
    public Integer updMeetingStatus(Integer id) {
        MeetingUser meetingUser = new MeetingUser();
        meetingUser.setId(id);
        //已读
        meetingUser.setMeetingStatus(2);
        return meetingUserMapper.updateByPrimaryKeySelective(meetingUser);
    }

    @Override
    public List<UserAckMsgVO> qryAttendUserAndMsgByStatus(Integer status) {
        return null;
    }

    @Override
    @CacheEvict(cacheNames = CaffeineConfig.MEETING_USERS, allEntries = true)
    public Integer deleteByMeetingId(String meetingId) {
        MeetingUserExample meetingUserExample = new MeetingUserExample();
        meetingUserExample.createCriteria().andMeetingIdEqualTo(meetingId);
        return meetingUserMapper.deleteByExample(meetingUserExample);
    }
}
